﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        V1DataContext DB = new V1DataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindddlschoolProvince();
            }
            if (ddlschoolName.Enabled)
            {
                bindTeachersDemo3();
            }
            else
            {
                bindgvTeacher();
            }
        }

        private void bindddlschoolProvince()
        {
            ddlschoolProvince.DataSource = DB.Schools.Select(S => S.province).Distinct();
            ddlschoolProvince.DataBind();
            ddlschoolProvince.Items.Insert(0, "请选择省份");
        }

        private void bindgvTeacher()
        {
            Int32? result = 0;
            DB.sp_GetPageData(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref result);
            AspNetPager1.RecordCount = result ?? 0;
            AspNetPager1.PageSize = 15;

            Int32? resultTemp = 0;
            gvTeacher.DataSource = DB.sp_GetPageData(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref resultTemp).Select(A => new
            {
                A.authorID,
                A.authorName,
                A.department,
                A.schoolID,
                A.schoolName,
                A.unit
            });
            gvTeacher.DataKeyNames = new string[] { "authorID", "schoolID" };
            gvTeacher.DataBind();
            #region 强制换行、正常换行
            //正常换行
            gvTeacher.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
            //强制换行
            gvTeacher.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            #endregion

        }

        private void bindTeachersDemo3()
        {
            Int32? result = 0;
            DB.sp_GetPageDataBySchoolName(ddlschoolName.SelectedValue, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex,ref result);
            AspNetPager1.RecordCount = result ?? 0;
            AspNetPager1.PageSize = 15;

            Int32? resultTemp = 0;
            gvTeacher.DataSource = DB.sp_GetPageDataBySchoolName(ddlschoolName.SelectedValue, AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex, ref resultTemp);
            gvTeacher.DataKeyNames = new string[] { "authorID", "schoolID" };
            gvTeacher.DataBind();
            #region 强制换行、正常换行
            //正常换行
            gvTeacher.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
            //强制换行
            gvTeacher.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            #endregion
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (ddlschoolName.Enabled)
            {
                bindTeachersDemo3();
            }
            else
            {
                bindgvTeacher();
            }
        }

        protected void gvTeacher_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标悬停变色，鼠标移开恢复颜色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor; this.style.backgroundColor='#ff5599';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG");
                //行编号
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void ddlschoolProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlschoolProvince.SelectedIndex != 0)
            {
                ddlschoolName.Enabled = true;
                ddlschoolName.Items.Clear();
                ddlschoolName.DataSource = DB.Schools.Where(S => S.province == ddlschoolProvince.SelectedItem.Text).Select(S => S.schoolName).Distinct();
                ddlschoolName.DataBind();
                AspNetPager1_PageChanged(sender, e);
            }
            else
            {
                ddlschoolName.Items.Clear();
                ddlschoolName.Enabled = false;
            }
        }

        protected void ddlschoolName_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(sender, e);
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            btnSelectAll.Text = btnSelectAll.Text == "全选" ? "反选" : "全选";

            foreach (GridViewRow gr in gvTeacher.Rows)
            {
                HtmlInputCheckBox chkItem = gr.Cells[1].FindControl("chkItem") as HtmlInputCheckBox;
                //相互变换
                chkItem.Checked = !chkItem.Checked;
            }
        }



    }
}